package com.cloud.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.dao.OrderDao;
import com.cloud.entity.Order;
import com.cloud.entity.Shop;
import com.cloud.feign.ShopFeign;
import com.cloud.feign.UserFeign;
import com.cloud.service.OrderService;
import com.cloud.util.JsonResult;
import com.cloud.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2020-11-28 11:08:40
 */
@Service("orderService")
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private ShopFeign shopFeign;


    @Override
    @Transactional
    @ShardingTransactionType(TransactionType.XA)
    public void createOrder() {

        //1332163743241576450
        Long userId = 1332163743241576450L;
        Order order = new Order();
        order.setUserId(userId);
        order.setCreateTime(new Date());
        order.setOrderInfo("测试事务.....Hello World111");
        order.setStatus(-1);
        order.insert();
        log.info("本地语句执行完毕");

        JsonResult jsonResult = shopFeign.find(1332606060905730049L);
        Shop shop = JsonUtil.parse(jsonResult.getData(), Shop.class);
        log.info("获取存库信息: {}",shop);
        //修改库存
        shopFeign.update(shop.getShopId());


        //修改用户金额
        userFeign.update(shop.getMoney(), userId);


        int i = 1/0;
    }
}